package com.foreknow.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Rule;
import com.foreknow.entity.Scheduling;
import com.foreknow.entity.SchedulingExample;
import com.foreknow.service.SchedulingService;
import com.foreknow.utils.Constance;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@Api
@RestController
@RequestMapping(value = "SchedulingController")
public class SchedulingController {

    @Autowired
    private SchedulingService schedulingService;


    //排班管理的首页
    @RequestMapping(value = "/querySchedulingForAll_inCurrentPage")
    public String querySchedulingForAll_inCurrentPage(@RequestBody Map<String, Object> para) throws Exception {
        String start2 = null;
        String end2 = null;
        Integer count = 0;
        Date date1 = null;
        Date date2 = null;

        Long start1 = (Long) para.get("start1");
        Long end1 = (Long) para.get("end1");
        if (start1 != null && end1 != null) {

            date1 = new Date(start1);
            date2 = new Date(end1);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            start2 = dateFormat.format(date1);
            end2 = dateFormat.format(date2);

        }

        Integer pageSize = (Integer) para.get("pageSize");
        Integer currentPage = (Integer) para.get("currentPage");

        count = schedulingService.countBySchedulingExample(date1, date2);
        List<Scheduling> list = schedulingService.querySchedulingForAll_inCurrentPage(start2, end2, pageSize, currentPage);

        HashMap<String, Object> hs = new HashMap<>();
        HashMap<String, Object> map = new HashMap<>();

        map.put("count", count);
        map.put("list", list);

        hs.put("data", map);
        hs.put("meta", new Constance("msg", 200));

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }


    @RequestMapping(value = "addScheduling")
    public String addScheduling(@RequestBody Map<String, Object> para) throws Exception {

        List<String> weeklist = (List<String>) para.get("weeklist");
        List<Integer> userIdlist = (List<Integer>) para.get("userIdlist");
        List<Integer> deptIdlist = (List<Integer>) para.get("deptIdlist");
        List<Integer> ruleIDlist = (List<Integer>) para.get("ruleIDlist");


//        下周没有安排的话就生成下周的排班,生成下一周的
        Date start = new Date();
        Date end = new Date();

//        SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");


        Calendar calendar = new GregorianCalendar();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        int nextMon;
        int nextSun;
        if (calendar.get(Calendar.DAY_OF_WEEK) == 1) {
            nextMon = 1;
            nextSun = 7;
        } else {
            nextMon = 7 - calendar.get(Calendar.DAY_OF_WEEK) + 2;
            nextSun = 7 - calendar.get(Calendar.DAY_OF_WEEK) + 2 + 6;
        }

        calendar.add(Calendar.DAY_OF_MONTH, nextMon);

        start = calendar.getTime();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, nextSun);
        end = calendar.getTime();


        for (int i = 0; i < userIdlist.size(); i++) {
            if (schedulingService.querySchedulingByTimeAndUserId(start, end, userIdlist.get(i)).size() == 0) {
                Scheduling scheduling = new Scheduling();
                scheduling.setDelmark(1);
                scheduling.setDeptid(deptIdlist.get(i));
                scheduling.setUserid(userIdlist.get(i));
                scheduling.setRuleid(ruleIDlist.get(i));


                calendar.setTime(start);
                for (int j = 0; j < 7; j++) {
                    char temp = weeklist.get(i).charAt(j * 2);

                    if (temp == 49) {
                        scheduling.setNoon("上午");
                        scheduling.setScheddate(calendar.getTime());
                        schedulingService.addScheduling(scheduling);
                    }
                    temp = weeklist.get(i).charAt(j * 2 + 1);
                    if (temp == 49) {
                        scheduling.setNoon("下午");
                        scheduling.setScheddate(calendar.getTime());
                        schedulingService.addScheduling(scheduling);
                    }

                    calendar.add(Calendar.DAY_OF_MONTH,1);
                }
            }

        }

//        List<Integer> list = new ArrayList<>();
//        for (int i = 0; i < week.size(); i++) {
//            Rule rule = new Rule();
//            rule.setDeptid(deptid);
//            rule.setWeek(week.get(i));
//            rule.setUserid(userId.get(i));
//            rule.setRulename(rulename);
//            rule.setDelmark(1);
//            int jjj = ruleService.addRule(rule);
//            list.add(jjj);
//        }

        HashMap<String, Object> hs = new HashMap<>();


//        hs.put("data", list);
        hs.put("meta", new Constance("msg", 200));

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}
